package com.tianlaiedu.service.impl;

import com.tianlaiedu.bean.User;
import com.tianlaiedu.dao.UserMapper;
import com.tianlaiedu.service.UserService;
import org.apache.ibatis.session.SqlSession;

/**
 * @author:hejing
 * @date:2021/11/26
 */
public class UserServiceImpl implements UserService {
    @Override
    public int addUser(User user) {
        try(SqlSession sqlSession = com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)) {
            final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            return userMapper.addUser(user);
        }
    }

    @Override
    public User findUser(String name, String pwd) {
        try (SqlSession sqlSession=com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)){
            final UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
            return userMapper.findUser(name,pwd);
        }

    }

    @Override
    public User findUser(Integer id) {
        try (SqlSession sqlSession=com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)){
            final UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
            return userMapper.findUserByid(id);
        }
    }

    @Override
    public User findUserByname(String name) {
        try (SqlSession sqlSession=com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)){
            final UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
            return userMapper.findUserByname(name);
        }
    }

    @Override
    public String findUsername(int id) {
        try (SqlSession sqlSession=com.tianlaiedu.utils.MapperUtil.getSqlSessionFactory().openSession(true)){
            final UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
            return userMapper.findUsername(id);
        }
    }


}
